package linkedlist;

/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-15
 * Time:14:39
 */

class Node {
    public int val;
    public Node next;

    public Node (int val) {
        this.val = val;
    }

}

public class SingLinkedList {
    public Node head;
    public int usedSize;
    //定义结点

    public void createList() {
        Node node1 = new Node(12);
        Node node2 = new Node(23);
        Node node3 = new Node(34);
        Node node4 = new Node(45);
        Node node5 = new Node(56);
        //int a = 10;
        node1.next = node2;//？
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
        this.usedSize = 5;
    }

    /**
     * 打印链表里面的元素
     */
    public void myToString(){
        Node cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if (key < 0 && key > usedSize) {
            return false;
        }
        Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到单链表的长度-》尝试不用我们定义的usedSize
    public int size(){
        int count = 0;
        Node cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
        //return usedSize;
    }


    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        }else {
            node.next = this.head;
            this.head = node;
        }
        usedSize++;
    }
    //尾插法
    public void addLast(int data){
        Node node = new Node(data);
        if (this.head == null) {
            this.head = node;
        }else {
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
        }
        usedSize++;
    }

    /**
     * 查找Index-1位置的节点的地址
     * @param index
     * @return
     */
    private Node searchIndex(int index) {
        Node cur = this.head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        /*for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }*/
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index > this.usedSize){
            System.out.println("数据错误!");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == this.usedSize) {
            addLast(data);
            return;
        }
        Node node = new Node(data);
        Node cur = searchIndex(index);
        node.next = cur.next;
        cur.next = node;
       this.usedSize++;//不能忘
    }


    /**
     * 找到关键字Key的前一个节点
     * @param key
     * @return 不存在 返回null 存在返回节点的地址
     */
    private Node findPrevNode(int key) {
        Node prev = this.head;
        while (prev.next != null) {
            if (prev.next.val == key) {
                return prev;
            }
            prev = prev.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){

        if(this.head == null) {
            return;
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            //this.head = null;//把头节点的引用置为空
            return;
        }

        Node prev = findPrevNode(key);
        if (prev == null) {
            throw new RuntimeException("数据异常!");
        }

        Node del = prev.next;
        prev.next  = del.next;
        usedSize--;
    }

    //删除所有值为key的节点
    //智能遍历一遍
    public void removeAllKey(int key){
        if (this.head == null) {
            return;
        }
        Node prev = this.head;
        Node cur = this.head.next;
        while (cur != null) {
            if (cur.next.val == key) {
                prev.next = cur.next;
                cur = cur.next;
                this.usedSize--;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (this.head.val == key) {
            this.head = this.head.next;
            this.usedSize--;
        }
    }


    public void clear(){
        Node cur = this.head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.usedSize = 0;
    }

}
